package com.hopu.oa.controller.management;

import com.hopu.oa.dao.management.MonthManageDao;
import com.hopu.oa.entity.management.AoaAttendsList;
import com.hopu.oa.entity.user.AoaUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class MonthManageController {
    @Autowired
    private MonthManageDao attendsService;
    private String starttime1;
    private String endtime1;
    private String month1;

    /**
     * 周报表
     */
    @RequestMapping("attendceweek")
    public String attendceweek(@RequestParam(value = "starttime", required = false, defaultValue = "") String starttime,
                               @RequestParam(value = "endtime", required = false, defaultValue = "") String endtime,
                               @RequestParam(value = "baseKey", required = false, defaultValue = "") String seachName,
                               @RequestParam(value = "page", defaultValue = "1") int page,
                               @RequestParam(value = "size", defaultValue = "2") int size,
                               Model model, HttpSession session) {
        if (starttime.equals("") && endtime.equals("")) {
            starttime = starttime1;
            endtime = endtime1;
        }

        AoaUser user = (AoaUser) session.getAttribute("user");
        List<String> weekday = new ArrayList<>();
        Collections.addAll(weekday, "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日");

        model.addAttribute("weekday", weekday);

        int totalElements = attendsService.selectListsByFatherId(user.getUserId(), starttime, endtime, seachName, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = attendsService.selectListsByFatherId(user.getUserId(), starttime, endtime, seachName, (page - 1) * size, size);

        HashMap<String, Integer> mapp = new HashMap<>();
        mapp.put("totalElements", totalElements);
        mapp.put("totalPages", totalPages);
        mapp.put("number", page);
        mapp.put("size", size);
        model.addAttribute("page", mapp);

        model.addAttribute("url", "realweektable");
        model.addAttribute("baseKey", seachName);
        model.addAttribute("starttime", starttime);
        model.addAttribute("endtime", endtime);

        if (userInfos.size() > 0) {
            ArrayList<Object> uList = new ArrayList<>();
            for (int i = 0; i < userInfos.size(); i++) {
                HashMap<String, Object> map = new HashMap<>();
                AoaUser userInfo = userInfos.get(i);
                map.put("deptName", userInfo.getDept().getDeptName());
                map.put("userName", userInfo.getUserName());
                map.put("userId", userInfo.getUserId());
                List<AoaAttendsList> attendsLists = attendsService.selectWeekByuid(userInfo.getUserId(), starttime, endtime);

                if (attendsLists.size() > 0) {
                    map.put("attendsList", attendsLists);
                }
                uList.add(map);
            }
            model.addAttribute("ulist", uList);

        }
        return "attendce/weektable";
    }

    @RequestMapping("realweektable")
    public String attendcaweekk(
            @RequestParam(value = "starttime", required = false, defaultValue = "") String starttime,
            @RequestParam(value = "endtime", required = false, defaultValue = "") String endtime,
            @RequestParam(value = "baseKey", required = false, defaultValue = "") String seachName,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "2") int size,
            Model model, HttpSession session) {

        System.out.println(22);
        System.out.println("starttime1 = " + starttime + " " + "endtime1 =" + endtime);

        System.out.println(seachName);

        if (starttime.equals("") && endtime.equals("")) {
            starttime = starttime1;
            endtime = endtime1;

        } else {
            starttime1 = starttime;
            endtime1 = endtime;
        }
        AoaUser user = (AoaUser) session.getAttribute("user");
        List<String> weekday = new ArrayList<>();
        Collections.addAll(weekday, "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日");

        model.addAttribute("weekday", weekday);

        int totalElements = attendsService.selectListsByFatherId(user.getUserId(), starttime, endtime, seachName, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = attendsService.selectListsByFatherId(user.getUserId(), starttime, endtime, seachName, (page - 1) * size, size);

        HashMap<String, Integer> mapp = new HashMap<>();
        mapp.put("totalElements", totalElements);
        mapp.put("totalPages", totalPages);
        mapp.put("number", page);
        mapp.put("size", size);
        model.addAttribute("page", mapp);

        model.addAttribute("url", "realweektable");
        model.addAttribute("baseKey", seachName);


        if (userInfos.size() > 0) {
            ArrayList<Object> uList = new ArrayList<>();
            for (int i = 0; i < userInfos.size(); i++) {
                HashMap<String, Object> map = new HashMap<>();
                AoaUser userInfo = userInfos.get(i);
                map.put("deptName", userInfo.getDept().getDeptName());
                map.put("userName", userInfo.getUserName());
                map.put("userId", userInfo.getUserId());
                List<AoaAttendsList> attendsLists = attendsService.selectWeekByuid(userInfo.getUserId(), starttime, endtime);

                if (attendsLists.size() > 0) {
                    map.put("attendsList", attendsLists);
                }
                uList.add(map);

            }
            model.addAttribute("ulist", uList);

        }


        return "attendce/realweektable";
    }


    /**
     * 考情月报表
     */
    @RequestMapping("attendcemonth")
    public String attendcemonth(@Param("starttime") String starttime,
                                @Param("endtime") String endtime,
                                @RequestParam(value = "baseKey", required = false, defaultValue = "") String seachName,
                                @RequestParam(value = "month", required = false, defaultValue = "") String month,
                                @RequestParam(value = "page", defaultValue = "1") int page,
                                @RequestParam(value = "size", defaultValue = "2") int size, HttpSession session, Model model) {
        System.out.println("month =" + month);
        System.out.println("starttime2 = " + starttime);
        System.out.println("endtime2 = " + endtime);

        if (month.equals("")) {
            month = month1;
        }
        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = attendsService.selectListsByFatherId(user.getUserId(), starttime, endtime, seachName, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> users = attendsService.selectListsByFatherId(user.getUserId(), starttime, endtime, seachName, (page - 1) * size, size);

        HashMap<String, Integer> mapp = new HashMap<>();
        mapp.put("totalElements", totalElements);
        mapp.put("totalPages", totalPages);
        mapp.put("number", page);
        mapp.put("size", size);
        model.addAttribute("page", mapp);

        model.addAttribute("url", "realmonthtable");
        model.addAttribute("baseKey", seachName);

        if (users.size() > 0) {
            ArrayList<Object> uList = new ArrayList<>();
            for (int i = 0; i < users.size(); i++) {
                AoaUser u = users.get(i);
                HashMap<Object, Object> map = new HashMap<>();
                map.put("userName", u.getUserName());
                map.put("deptName", u.getDept().getDeptName());
                List<Map> maps = attendsService.selectMonthByuid(u.getUserId(), month);
                int[] checkingIn = new int[6];
                for (int j = 0; j < maps.size(); j++) {
                    Map<Object, Object> objectMap = maps.get(j);
                    if (objectMap.get("statusName").equals("正常")) {
                        checkingIn[0]++;
                    } else if (objectMap.get("statusName").equals("迟到")) {
                        checkingIn[1]++;
                    } else if (objectMap.get("statusName").equals("早退")) {
                        checkingIn[2]++;
                    } else if (objectMap.get("statusName").equals("旷工")) {
                        checkingIn[5]++;
                    }
                    if (objectMap.get("typeName").equals("请假")) {
                        checkingIn[3]++;
                    } else if (objectMap.get("typeName").equals("出差")) {
                        checkingIn[4]++;
                    }
                }
                map.put("checkingIn", checkingIn);
                uList.add(map);
            }
            model.addAttribute("uList", uList);
        }
        return "attendce/monthtable";
    }


    @RequestMapping("realmonthtable")
    public String attendcemonths(@Param("starttime") String starttime,
                                 @Param("endtime") String endtime,
                                 @RequestParam(value = "baseKey", required = false, defaultValue = "") String seachName,
                                 @RequestParam(value = "month", required = false, defaultValue = "") String month,
                                 @RequestParam(value = "page", defaultValue = "1") int page,
                                 @RequestParam(value = "size", defaultValue = "2") int size, HttpSession session, Model model) {
        System.out.println(33);
        System.out.println("starttime2 = " + starttime);
        System.out.println("endtime2 = " + endtime);
        System.out.println("month =" + month);
        if (month.equals("")) {
            month = month1;
        } else {
            month1 = month;
        }
        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = attendsService.selectListsByFatherId(user.getUserId(), starttime, endtime, seachName, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> users = attendsService.selectListsByFatherId(user.getUserId(), starttime, endtime, seachName, (page - 1) * size, size);

        HashMap<String, Integer> mapp = new HashMap<>();
        mapp.put("totalElements", totalElements);
        mapp.put("totalPages", totalPages);
        mapp.put("number", page);
        mapp.put("size", size);
        model.addAttribute("page", mapp);

        model.addAttribute("url", "realmonthtable");
        model.addAttribute("baseKey", seachName);

        if (users.size() > 0) {
            ArrayList<Object> uList = new ArrayList<>();
            for (int i = 0; i < users.size(); i++) {
                AoaUser u = users.get(i);
                HashMap<Object, Object> map = new HashMap<>();
                map.put("userName", u.getUserName());
                map.put("deptName", u.getDept().getDeptName());
                List<Map> maps = attendsService.selectMonthByuid(u.getUserId(), month);
                int[] checkingIn = new int[6];
                for (int j = 0; j < maps.size(); j++) {
                    Map<Object, Object> objectMap = maps.get(j);
                    if (objectMap.get("statusName").equals("正常")) {
                        checkingIn[0]++;
                    } else if (objectMap.get("statusName").equals("迟到")) {
                        checkingIn[1]++;
                    } else if (objectMap.get("statusName").equals("早退")) {
                        checkingIn[2]++;
                    } else if (objectMap.get("statusName").equals("旷工")) {
                        checkingIn[5]++;
                    }
                    if (objectMap.get("typeName").equals("请假")) {
                        checkingIn[3]++;
                    } else if (objectMap.get("typeName").equals("出差")) {
                        checkingIn[4]++;
                    }
                }
                map.put("checkingIn", checkingIn);
                uList.add(map);
            }
            model.addAttribute("uList", uList);
        }
        return "attendce/realmonthtable";
    }

}
